package com.jiyuren.spring.utils;

import redis.clients.jedis.Jedis;

/**
 * Created by Administrator on 2017/3/22.
 */
public class RedisHelper {

    private Jedis mJedis;
    private static volatile RedisHelper mInstance;

    public static RedisHelper getInstance() {
        if (null == mInstance) {
            synchronized (RedisHelper.class) {
                if (null == mInstance) {
                    mInstance = new RedisHelper();
                }
            }
        }
        return mInstance;
    }

    private RedisHelper() {
        reConnRedis();
    }

    public void reConnRedis() {
        if (null == mJedis) {
            mJedis = new Jedis("127.0.0.1");
        }
    }

    public void set(String key, String value) {
        reConnRedis();
        mJedis.set(key, value);
    }

    public String get(String key) {
        reConnRedis();
        return mJedis.get(key);
    }

    public void disConnect() {
        reConnRedis();
//        mJedis.close();
        mJedis.disconnect();
    }

    public static void main(String[] args) {
        RedisHelper redisHelper = RedisHelper.getInstance();
        redisHelper.set("name", "lisi");
        String value = redisHelper.get("name");
        String text = redisHelper.get("text");
        System.out.println(value + text);
    }

}
